#include "../include/my_fs.hpp"
#include "../include/bambu.hpp"
#include "../include/my_ota.hpp"
#include "../include/my_wifi.hpp"


#define LITTLEFS_DEBUG 0

static void restoreFactory(String path){
    if(LittleFS.remove(path)){
        Serial.println("SUCCESS!");ESP.restart();
    }
}

static JsonDocument getData(String path){
    File file = LittleFS.open(path, "r");
    JsonDocument Tdata;
    deserializeJson(Tdata, file);
    return Tdata;
}

static void writeData(JsonDocument Tdata, String path){
    File file = LittleFS.open(path, "w");
    serializeJson(Tdata, file);
    file.close();
}

static void init_fs(void){
    if (!LittleFS.begin()) {
    Serial.println("LittleFS Mount Failed. Formatting...");
    if (LittleFS.format()) {
        Serial.println("LittleFS formatted successfully.");
        LittleFS.begin() ?  Serial.println("LittleFS mounted successfully after format."):
                            Serial.println("LittleFS mount failed even after format.");
    }
    else
        Serial.println("LittleFS format failed!");
    }
    if(!LittleFS.exists("/data.json")){
        JsonDocument Pdata;
        Pdata["lastFilament"] = "0";
        writeData(Pdata,"/data.json");
    }
}

static void get_or_save(void){
  if (!LittleFS.exists("/config.json") || LITTLEFS_DEBUG){
    JsonDocument Cdata;
    Serial.println("1.请输入wifi名:");
    while(!(Serial.available() > 0))delay(100);
    my_wifi.cfg->name = Serial.readString();

    Serial.println("2.请输入wifi密码:");
    while(!(Serial.available() > 0))delay(100);
    my_wifi.cfg->passwd = Serial.readString();

    Serial.println("3.请输入打印机地址:");
    while(!(Serial.available() > 0))delay(100);
    bambu.cfg->mqtt_broker = Serial.readString();

    Serial.println("4.请输入打印机配对码:");
    while(!(Serial.available() > 0))delay(100);
    bambu.cfg->mqtt_password = Serial.readString();

    Serial.println("5.请输入打印机序列号:");
    while(!(Serial.available() > 0))delay(100);
    bambu.cfg->device_serial = Serial.readString();

    Serial.println("6.请输入OTA秘钥:");
    while(!(Serial.available() > 0))delay(100);
    my_ota.cfg->passwd = Serial.readString();

    Cdata["wifiName"] = my_wifi.cfg->name;
    Cdata["wifiKey"]  = my_wifi.cfg->passwd;
    Cdata["bambu_mqtt_broker"]    = bambu.cfg->mqtt_broker;
    Cdata["bambu_mqtt_password"]  = bambu.cfg->mqtt_password;
    Cdata["bambu_device_serial"]  = bambu.cfg->device_serial;
    Cdata["ota_password"]         = my_ota.cfg->passwd;
    writeData(Cdata, "/config.json");
  }
  else{
    JsonDocument Cdata = getData("/config.json");
    serializeJsonPretty(Cdata,Serial);
    my_wifi.cfg->name         = Cdata["wifiName"].as<String>();
    my_wifi.cfg->passwd       = Cdata["wifiKey"].as<String>();
    bambu.cfg->mqtt_broker    = Cdata["bambu_mqtt_broker"].as<String>();
    bambu.cfg->mqtt_password  = Cdata["bambu_mqtt_password"].as<String>();
    bambu.cfg->device_serial  = Cdata["bambu_device_serial"].as<String>();
    my_ota.cfg->passwd        = Cdata["ota_password"].as<String>();
  }
}

static MY_FS_OPS ops = {
    .init   = init_fs,
    .write  = writeData,
    .read   = getData,
    .work   = get_or_save,
    .clean  = restoreFactory,
};

MY_FS my_fs = {
    .ops = &ops,
};


